# FUNCS:
# ...dl: 1080p
# ...dlp: 1080p TOR
# ...dls: 1080p SHORT NAME
# ...best: best
# ...bestp: best TOR
# ...1080: 1080p
# ...1080s: 1080p SHORT NAME
# ...720: 720p
# ...720p: 720p TOR
# ...720s: 720p SHORT NAME

# ...small: 360p
# ...smalls: 360p SHORT NAME
# ...640: 360p
# ...360: 360p
# ...360p: 360p TOR

alias dl="1080"
alias dlp="1080 --proxy socks5://localhost:9050"
alias dls="1080s"


# https://github.com/yt-dlp/yt-dlp#output-template
SHORT_FORMAT='(%(channel_id)s) %(upload_date>%Y %m %d)s [%(id)s].%(ext)s'

# https://github.com/yt-dlp/yt-dlp/issues/1003#issuecomment-922005573
# Max is 255 bytes on most systems
# Need space for other parts of filename
# Orig title in metadata
LONG_FORMAT='(%(channel)s) %(upload_date>%Y %m %d)s - %(title).150B [%(id)s].%(ext)s'


# SUBS
# # https://github.com/yt-dlp/yt-dlp/issues/630#issuecomment-893659460

# SET TITLE INTO TITLE_ORIG: --parse-metadata "title:(?s)(?P<meta_title_orig>.+)"
# UNSET TITLE: --parse-metadata ":(?P<meta_title>)"

# S: SHORT
# F: FUNC
alias 1080="1080f -o \"$LONG_FORMAT\""
alias 1080s="1080f -o \"$SHORT_FORMAT\""
1080f () {
  yt-dlp -N 5 --no-mtime \
  --retries infinite --fragment-retries infinite \
  -f 'bestvideo[width<=1920][height<=1920][fps<=60]+bestaudio/best[width<=1920][height<=1920][fps<=60]' \
  --write-subs --write-auto-subs --embed-subs --compat-options no-keep-subs --sub-format ttml --convert-subs vtt --sub-lang 'en,.*-orig' \
  --paths "temp:tmp" \
  --remux-video mkv \
  --parse-metadata "title:(?s)(?P<meta_title_orig>.+)" \
  --parse-metadata ":(?P<meta_title>)" \
  --parse-metadata ":(?P<meta_comment>)" \
  --parse-metadata "description:(?s)(?P<meta_comment>.+)" \
  --parse-metadata "%(upload_date>%Y-%m-%d)s:(?s)(?P<meta_date>.+)" \
  --parse-metadata "location:(?s)(?P<meta_location>.+)" \
  --parse-metadata "view_count:(?s)(?P<meta_view_count>.+)" \
  --parse-metadata "live_status:(?s)(?P<meta_live_status>.+)" \
  --parse-metadata "like_count:(?s)(?P<meta_like_count>.+)" \
  --parse-metadata "dislike_count:(?s)(?P<meta_dislike_count>.+)" \
  --parse-metadata "comment_count:(?s)(?P<meta_comment_count>.+)" \
  --parse-metadata "%(release_timestamp>%A %Y-%m-%d %H\:%M\:%S %z)s:(?s)(?P<meta_release_timestamp>.+)" \
  --parse-metadata "uploader_url:(?s)(?P<meta_uploader_url>.+)" \
  --parse-metadata "channel_url:(?s)(?P<meta_channel_url>.+)" \
  --embed-metadata \
  --embed-chapters \
  "$@"
}

alias 720="720f -o \"$LONG_FORMAT\""
alias 720p="720 --proxy socks5://localhost:9050"
alias 720s="720f -o \"$SHORT_FORMAT\""
720f () {
  yt-dlp -N 5 --no-mtime \
  --retries infinite --fragment-retries infinite \
  -f 'bestvideo[width<=1280][height<=1280][fps<=45]+bestaudio/best[width<=1280][height<=1280][fps<=45]' \
  --write-subs --write-auto-subs --embed-subs --compat-options no-keep-subs --sub-format ttml --convert-subs vtt --sub-lang 'en,.*-orig' \
  --paths "temp:tmp" \
  --remux-video mkv \
  --parse-metadata "title:(?s)(?P<meta_title_orig>.+)" \
  --parse-metadata ":(?P<meta_title>)" \
  --parse-metadata ":(?P<meta_comment>)" \
  --parse-metadata "description:(?s)(?P<meta_comment>.+)" \
  --parse-metadata "%(upload_date>%Y-%m-%d)s:(?s)(?P<meta_date>.+)" \
  --parse-metadata "location:(?s)(?P<meta_location>.+)" \
  --parse-metadata "view_count:(?s)(?P<meta_view_count>.+)" \
  --parse-metadata "live_status:(?s)(?P<meta_live_status>.+)" \
  --parse-metadata "like_count:(?s)(?P<meta_like_count>.+)" \
  --parse-metadata "dislike_count:(?s)(?P<meta_dislike_count>.+)" \
  --parse-metadata "comment_count:(?s)(?P<meta_comment_count>.+)" \
  --parse-metadata "%(release_timestamp>%A %Y-%m-%d %H\:%M\:%S %z)s:(?s)(?P<meta_release_timestamp>.+)" \
  --parse-metadata "uploader_url:(?s)(?P<meta_uploader_url>.+)" \
  --parse-metadata "channel_url:(?s)(?P<meta_channel_url>.+)" \
  --embed-metadata \
  --embed-chapters \
  "$@"
}

alias best="bestf -o \"$LONG_FORMAT\""
alias bestp="best --proxy socks5://localhost:9050"
alias bests="bestf -o \"$SHORT_FORMAT\""
bestf () {
  yt-dlp -N 5 --no-mtime \
  --retries infinite --fragment-retries infinite \
  -f 'bestvideo+bestaudio/best' \
  --write-subs --write-auto-subs --embed-subs --compat-options no-keep-subs --sub-format ttml --convert-subs vtt --sub-lang 'en,.*-orig' \
  --paths "temp:tmp" \
  --remux-video mkv \
  --parse-metadata "title:(?s)(?P<meta_title_orig>.+)" \
  --parse-metadata ":(?P<meta_title>)" \
  --parse-metadata ":(?P<meta_comment>)" \
  --parse-metadata "description:(?s)(?P<meta_comment>.+)" \
  --parse-metadata "%(upload_date>%Y-%m-%d)s:(?s)(?P<meta_date>.+)" \
  --parse-metadata "location:(?s)(?P<meta_location>.+)" \
  --parse-metadata "view_count:(?s)(?P<meta_view_count>.+)" \
  --parse-metadata "live_status:(?s)(?P<meta_live_status>.+)" \
  --parse-metadata "like_count:(?s)(?P<meta_like_count>.+)" \
  --parse-metadata "dislike_count:(?s)(?P<meta_dislike_count>.+)" \
  --parse-metadata "comment_count:(?s)(?P<meta_comment_count>.+)" \
  --parse-metadata "%(release_timestamp>%A %Y-%m-%d %H\:%M\:%S %z)s:(?s)(?P<meta_release_timestamp>.+)" \
  --parse-metadata "uploader_url:(?s)(?P<meta_uploader_url>.+)" \
  --parse-metadata "channel_url:(?s)(?P<meta_channel_url>.+)" \
  --embed-metadata \
  --embed-chapters \
  "$@"
}

alias small="640"
alias 640="360"
alias 360="360f -o \"$LONG_FORMAT\""
alias 360p="360 --proxy socks5://localhost:9050"
alias smalls="360f -o \"$SHORT_FORMAT\""
360f () {
  yt-dlp -N 5 --no-mtime \
  --retries infinite --fragment-retries infinite \
  -f 'bestvideo[width<=640][height<=640][fps<=45]+bestaudio/best[width<=640][height<=640][fps<=45]' \
  --write-subs --write-auto-subs --embed-subs --compat-options no-keep-subs --sub-format ttml --convert-subs vtt --sub-lang 'en,.*-orig' \
  --paths "temp:tmp" \
  --remux-video mkv \
  --parse-metadata "title:(?s)(?P<meta_title_orig>.+)" \
  --parse-metadata ":(?P<meta_title>)" \
  --parse-metadata ":(?P<meta_comment>)" \
  --parse-metadata "description:(?s)(?P<meta_comment>.+)" \
  --parse-metadata "%(upload_date>%Y-%m-%d)s:(?s)(?P<meta_date>.+)" \
  --parse-metadata "location:(?s)(?P<meta_location>.+)" \
  --parse-metadata "view_count:(?s)(?P<meta_view_count>.+)" \
  --parse-metadata "live_status:(?s)(?P<meta_live_status>.+)" \
  --parse-metadata "like_count:(?s)(?P<meta_like_count>.+)" \
  --parse-metadata "dislike_count:(?s)(?P<meta_dislike_count>.+)" \
  --parse-metadata "comment_count:(?s)(?P<meta_comment_count>.+)" \
  --parse-metadata "%(release_timestamp>%A %Y-%m-%d %H\:%M\:%S %z)s:(?s)(?P<meta_release_timestamp>.+)" \
  --parse-metadata "uploader_url:(?s)(?P<meta_uploader_url>.+)" \
  --parse-metadata "channel_url:(?s)(?P<meta_channel_url>.+)" \
  --embed-metadata \
  --embed-chapters \
  "$@"
}

alias ls="ls -lhrt --color"


# ONLY TEMP FIX
# REBUILD FOR FINAL
alias update="pip install -U yt-dlp"



# PARAM 1: URL
# PARAM 2: THREADS
# dl () {
#   yt-dlp -N "${2:-5}" --no-mtime -f 'bestvideo+bestaudio/best' "$1"
# }

# PARAM 1: URL
# PARAM 2: THREADS
# yt720 () {
#   yt-dlp -N "${2:-5}" --no-mtime -f 'bestvideo[height<=720][fps<=45]+bestaudio/best[height<=720][fps<=45]' "$1"
# }
